/* This file is part of Multorg.

Multorg is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

Multorg is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Foobar; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Threading;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
using Un4seen.Bass;
using Un4seen.Bass.AddOn.Wma;
using Un4seen.Bass.AddOn.Tags;

namespace RadioPlugin
{



	/// <summary>
	/// Summary description for ctlMain.
	/// </summary>
	public class ctlRadio : System.Windows.Forms.UserControl
    {
		// PINNED
        private string _myUserAgent = "RADIO42";
        [FixedAddressValueType()]
        public IntPtr _myUserAgentPtr;

		// LOCAL VARS
		private int _Stream = 0;
		private string _url = String.Empty;
		private DOWNLOADPROC myStreamCreateURL;
		private TAG_INFO _tagInfo;
		private SYNCPROC mySync;
		private RECORDPROC myRecProc;
		private int _wmaPlugIn = 0;
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components;
        private OpenFileDialog openFileDialog1;
        
		public ctlRadio()
		{
			
            // This call is required by the Windows.Forms Form Designer.
			InitializeComponent();
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
			Bass.BASS_PluginFree( _wmaPlugIn );
			// close bass
			Bass.BASS_Stop();
			Bass.BASS_Free();

          Bass.BASS_PluginFree(_wmaPlugIn);
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
			this.tmrNetRadio = new System.Windows.Forms.Timer(this.components);
			this.textBoxYear = new System.Windows.Forms.TextBox();
			this.textBoxGenre = new System.Windows.Forms.TextBox();
			this.textBoxComment = new System.Windows.Forms.TextBox();
			this.textBoxAlbum = new System.Windows.Forms.TextBox();
			this.textBoxTitle = new System.Windows.Forms.TextBox();
			this.textBoxArtist = new System.Windows.Forms.TextBox();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.statusBar1 = new System.Windows.Forms.StatusBar();
			this.menuStrip1 = new System.Windows.Forms.MenuStrip();
			this.PolskaStacjaMI = new System.Windows.Forms.ToolStripMenuItem();
			this.PS1MI = new System.Windows.Forms.ToolStripMenuItem();
			this.PS2MI = new System.Windows.Forms.ToolStripMenuItem();
			this.PS3MI = new System.Windows.Forms.ToolStripMenuItem();
			this.PS4MI = new System.Windows.Forms.ToolStripMenuItem();
			this.PS5MI = new System.Windows.Forms.ToolStripMenuItem();
			this.PS6MI = new System.Windows.Forms.ToolStripMenuItem();
			this.PS7MI = new System.Windows.Forms.ToolStripMenuItem();
			this.PS8MI = new System.Windows.Forms.ToolStripMenuItem();
			this.MuzykaFilmowaMI = new System.Windows.Forms.ToolStripMenuItem();
			this.MF1MI = new System.Windows.Forms.ToolStripMenuItem();
			this.MF2MI = new System.Windows.Forms.ToolStripMenuItem();
			this.menuStrip1.SuspendLayout();
			this.SuspendLayout();
			// 
			// openFileDialog1
			// 
			this.openFileDialog1.FileName = "openFileDialog1";
			// 
			// tmrNetRadio
			// 
			this.tmrNetRadio.Interval = 50;
			// 
			// textBoxYear
			// 
			this.textBoxYear.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
									| System.Windows.Forms.AnchorStyles.Right)));
			this.textBoxYear.BackColor = System.Drawing.SystemColors.Control;
			this.textBoxYear.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.textBoxYear.Location = new System.Drawing.Point(3, 360);
			this.textBoxYear.Name = "textBoxYear";
			this.textBoxYear.Size = new System.Drawing.Size(513, 13);
			this.textBoxYear.TabIndex = 25;
			this.textBoxYear.Text = "Year";
			// 
			// textBoxGenre
			// 
			this.textBoxGenre.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
									| System.Windows.Forms.AnchorStyles.Right)));
			this.textBoxGenre.BackColor = System.Drawing.SystemColors.Control;
			this.textBoxGenre.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.textBoxGenre.Location = new System.Drawing.Point(3, 336);
			this.textBoxGenre.Name = "textBoxGenre";
			this.textBoxGenre.Size = new System.Drawing.Size(513, 13);
			this.textBoxGenre.TabIndex = 24;
			this.textBoxGenre.Text = "Genre";
			// 
			// textBoxComment
			// 
			this.textBoxComment.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
									| System.Windows.Forms.AnchorStyles.Right)));
			this.textBoxComment.BackColor = System.Drawing.SystemColors.Control;
			this.textBoxComment.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.textBoxComment.Location = new System.Drawing.Point(3, 312);
			this.textBoxComment.Name = "textBoxComment";
			this.textBoxComment.Size = new System.Drawing.Size(513, 13);
			this.textBoxComment.TabIndex = 23;
			this.textBoxComment.Text = "Comment";
			// 
			// textBoxAlbum
			// 
			this.textBoxAlbum.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
									| System.Windows.Forms.AnchorStyles.Right)));
			this.textBoxAlbum.BackColor = System.Drawing.SystemColors.Control;
			this.textBoxAlbum.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.textBoxAlbum.Location = new System.Drawing.Point(3, 288);
			this.textBoxAlbum.Name = "textBoxAlbum";
			this.textBoxAlbum.Size = new System.Drawing.Size(513, 13);
			this.textBoxAlbum.TabIndex = 22;
			this.textBoxAlbum.Text = "Album";
			// 
			// textBoxTitle
			// 
			this.textBoxTitle.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
									| System.Windows.Forms.AnchorStyles.Right)));
			this.textBoxTitle.BackColor = System.Drawing.SystemColors.Control;
			this.textBoxTitle.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.textBoxTitle.Location = new System.Drawing.Point(3, 264);
			this.textBoxTitle.Name = "textBoxTitle";
			this.textBoxTitle.Size = new System.Drawing.Size(513, 13);
			this.textBoxTitle.TabIndex = 21;
			this.textBoxTitle.Text = "Title";
			// 
			// textBoxArtist
			// 
			this.textBoxArtist.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
									| System.Windows.Forms.AnchorStyles.Right)));
			this.textBoxArtist.BackColor = System.Drawing.SystemColors.Control;
			this.textBoxArtist.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.textBoxArtist.Location = new System.Drawing.Point(3, 240);
			this.textBoxArtist.Name = "textBoxArtist";
			this.textBoxArtist.Size = new System.Drawing.Size(513, 13);
			this.textBoxArtist.TabIndex = 20;
			this.textBoxArtist.Text = "Artist";
			// 
			// textBox1
			// 
			this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
									| System.Windows.Forms.AnchorStyles.Left)));
			this.textBox1.Location = new System.Drawing.Point(3, 141);
			this.textBox1.MaxLength = 2032767;
			this.textBox1.Multiline = true;
			this.textBox1.Name = "textBox1";
			this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
			this.textBox1.Size = new System.Drawing.Size(260, 78);
			this.textBox1.TabIndex = 18;
			this.textBox1.WordWrap = false;
			// 
			// statusBar1
			// 
			this.statusBar1.Location = new System.Drawing.Point(0, 451);
			this.statusBar1.Name = "statusBar1";
			this.statusBar1.Size = new System.Drawing.Size(700, 22);
			this.statusBar1.TabIndex = 26;
			// 
			// menuStrip1
			// 
			this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
									this.PolskaStacjaMI,
									this.MuzykaFilmowaMI});
			this.menuStrip1.Location = new System.Drawing.Point(0, 0);
			this.menuStrip1.Name = "menuStrip1";
			this.menuStrip1.Size = new System.Drawing.Size(700, 24);
			this.menuStrip1.TabIndex = 27;
			this.menuStrip1.Text = "menuStrip1";
			// 
			// PolskaStacjaMI
			// 
			this.PolskaStacjaMI.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
									this.PS1MI,
									this.PS2MI,
									this.PS3MI,
									this.PS4MI,
									this.PS5MI,
									this.PS6MI,
									this.PS7MI,
									this.PS8MI});
			this.PolskaStacjaMI.Name = "PolskaStacjaMI";
			this.PolskaStacjaMI.Size = new System.Drawing.Size(79, 20);
			this.PolskaStacjaMI.Text = "&PolskaStacja";
			// 
			// PS1MI
			// 
			this.PS1MI.ImageTransparentColor = System.Drawing.Color.Magenta;
			this.PS1MI.Name = "PS1MI";
			this.PS1MI.Size = new System.Drawing.Size(243, 22);
			this.PS1MI.Tag = "http://www.polskastacja.pl/play/hot100.pls";
			this.PS1MI.Text = "HOT 100 - Goraca Setka Nowosci";
			this.PS1MI.Click += new System.EventHandler(this.Listen_Click);
			// 
			// PS2MI
			// 
			this.PS2MI.ImageTransparentColor = System.Drawing.Color.Magenta;
			this.PS2MI.Name = "PS2MI";
			this.PS2MI.Size = new System.Drawing.Size(243, 22);
			this.PS2MI.Tag = "http://www.polskastacja.pl/play/mnt_hq.pls";
			this.PS2MI.Text = "Muzyka Na TOPIE";
			this.PS2MI.Click += new System.EventHandler(this.Listen_Click);
			// 
			// PS3MI
			// 
			this.PS3MI.Name = "PS3MI";
			this.PS3MI.Size = new System.Drawing.Size(243, 22);
			this.PS3MI.Tag = "http://www.polskastacja.pl/play/all_hq.pls";
			this.PS3MI.Text = "Tylko Polskie Przeboje";
			this.PS3MI.Click += new System.EventHandler(this.Listen_Click);
			// 
			// PS4MI
			// 
			this.PS4MI.Name = "PS4MI";
			this.PS4MI.Size = new System.Drawing.Size(243, 22);
			this.PS4MI.Tag = "http://www.polskastacja.pl/play/np_hq.pls";
			this.PS4MI.Text = "Najwieksze Przeboje 80 90";
			this.PS4MI.Click += new System.EventHandler(this.Listen_Click);
			// 
			// PS5MI
			// 
			this.PS5MI.Name = "PS5MI";
			this.PS5MI.Size = new System.Drawing.Size(243, 22);
			this.PS5MI.Tag = "http://www.polskastacja.pl/play/oldieshq.pls";
			this.PS5MI.Text = "Najwieksze Przeboje 60 70";
			this.PS5MI.Click += new System.EventHandler(this.Listen_Click);
			// 
			// PS6MI
			// 
			this.PS6MI.Name = "PS6MI";
			this.PS6MI.Size = new System.Drawing.Size(243, 22);
			this.PS6MI.Tag = "http://www.polskastacja.pl/play/pnp_hq.pls";
			this.PS6MI.Text = "Polskie Niezapomniane Przeboje";
			this.PS6MI.Click += new System.EventHandler(this.Listen_Click);
			// 
			// PS7MI
			// 
			this.PS7MI.Name = "PS7MI";
			this.PS7MI.Size = new System.Drawing.Size(243, 22);
			this.PS7MI.Tag = "http://www.polskastacja.pl/play/lovehq.pls";
			this.PS7MI.Text = "Przeboje We Dwoje";
			this.PS7MI.Click += new System.EventHandler(this.Listen_Click);
			// 
			// PS8MI
			// 
			this.PS8MI.Name = "PS8MI";
			this.PS8MI.Size = new System.Drawing.Size(243, 22);
			this.PS8MI.Tag = "http://www.polskastacja.pl/play/piosenkifilmowe.pls";
			this.PS8MI.Text = "Piosenki z Filmow";
			this.PS8MI.Click += new System.EventHandler(this.Listen_Click);
			// 
			// MuzykaFilmowaMI
			// 
			this.MuzykaFilmowaMI.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
									this.MF1MI,
									this.MF2MI});
			this.MuzykaFilmowaMI.Name = "MuzykaFilmowaMI";
			this.MuzykaFilmowaMI.Size = new System.Drawing.Size(96, 20);
			this.MuzykaFilmowaMI.Text = "&Muzyka Filmowa";
			// 
			// MF1MI
			// 
			this.MF1MI.Name = "MF1MI";
			this.MF1MI.Size = new System.Drawing.Size(166, 22);
			this.MF1MI.Tag = "http://www.polskastacja.pl/play/piosenkifilmowe.pls";
			this.MF1MI.Text = "Piosenki z Filmow";
			this.MF1MI.Click += new System.EventHandler(this.Listen_Click);
			// 
			// MF2MI
			// 
			this.MF2MI.Name = "MF2MI";
			this.MF2MI.Size = new System.Drawing.Size(166, 22);
			this.MF2MI.Tag = "http://www.polskastacja.pl/play/film.pls";
			this.MF2MI.Text = "Muzyka filmowa";
			this.MF2MI.Click += new System.EventHandler(this.Listen_Click);
			// 
			// ctlRadio
			// 
			this.Controls.Add(this.statusBar1);
			this.Controls.Add(this.textBoxYear);
			this.Controls.Add(this.textBoxGenre);
			this.Controls.Add(this.textBoxComment);
			this.Controls.Add(this.textBoxAlbum);
			this.Controls.Add(this.textBoxTitle);
			this.Controls.Add(this.textBoxArtist);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.menuStrip1);
			this.Name = "ctlRadio";
			this.Size = new System.Drawing.Size(700, 473);
			this.Load += new System.EventHandler(this.CtlRadio_Load);
			this.menuStrip1.ResumeLayout(false);
			this.menuStrip1.PerformLayout();
			this.ResumeLayout(false);
			this.PerformLayout();
		}
		private System.Windows.Forms.ToolStripMenuItem PS1MI;
		private System.Windows.Forms.ToolStripMenuItem PS2MI;
		private System.Windows.Forms.ToolStripMenuItem PS3MI;
		private System.Windows.Forms.ToolStripMenuItem PS4MI;
		private System.Windows.Forms.ToolStripMenuItem PS5MI;
		private System.Windows.Forms.ToolStripMenuItem PS6MI;
		private System.Windows.Forms.ToolStripMenuItem PS7MI;
		private System.Windows.Forms.ToolStripMenuItem PS8MI;
		private System.Windows.Forms.ToolStripMenuItem MF1MI;
		private System.Windows.Forms.ToolStripMenuItem MF2MI;
		private System.Windows.Forms.ToolStripMenuItem MuzykaFilmowaMI;
		private System.Windows.Forms.ToolStripMenuItem PolskaStacjaMI;
		private System.Windows.Forms.MenuStrip menuStrip1;
		private System.Windows.Forms.StatusBar statusBar1;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.TextBox textBoxArtist;
		private System.Windows.Forms.TextBox textBoxTitle;
		private System.Windows.Forms.TextBox textBoxAlbum;
		private System.Windows.Forms.TextBox textBoxComment;
		private System.Windows.Forms.TextBox textBoxGenre;
		private System.Windows.Forms.TextBox textBoxYear;
		private System.Windows.Forms.Timer tmrNetRadio;
		#endregion

          

        ~ctlRadio() {
           
        }
        
               

        private void Loadbutton_Click(object sender, EventArgs e)
        {
           
        }

        private void Playbutton_Click(object sender, EventArgs e)
        {
         
        }

        private void STOPbutton_Click(object sender, EventArgs e)
        {
           
        }

        private void Gobutton_Click(object sender, EventArgs e)
        {
            
        }	

				
		
        
		
		void CtlRadio_Load(object sender, EventArgs e)
		{
			//BassNet.Registration("your email", "your regkey");

			// check the version..
			if (Utils.HighWord(Bass.BASS_GetVersion()) != Bass.BASSVERSION)
			{
				MessageBox.Show( this, "Wrong Bass Version!" );
			}

			// stupid thing here as well, just to demo...
			//string userAgent = Bass.BASS_GetConfigString(BASSConfig.BASS_CONFIG_NET_AGENT);

            Bass.BASS_SetConfigPtr(BASSConfig.BASS_CONFIG_NET_AGENT, _myUserAgentPtr);
			
            Bass.BASS_SetConfig(BASSConfig.BASS_CONFIG_NET_PREBUF, 0); // so that we can display the buffering%
            Bass.BASS_SetConfig(BASSConfig.BASS_CONFIG_NET_PLAYLIST, 1);

			if ( Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_DEFAULT, this.Handle) )
			{
				// Some words about loading add-ons:
				// In order to set an add-on option with BASS_SetConfig, we need to make sure, that the
				// library (in this case basswma.dll) is actually loaded!
				// However, an external library is dynamically loaded in .NET with the first call 
				// to one of it's methods...
				// As BASS will only know about additional config options once the lib has been loaded,
				// we need to make sure, that the lib is loaded before we make the following call.
				// 1) Loading a lib manually :
				// BassWma.LoadMe();  // basswma.dll must be in same directory
				// 2) Using the BASS PlugIn system (recommended):
				_wmaPlugIn = Bass.BASS_PluginLoad( "basswma.dll" );
				// 3) ALTERNATIVLY you might call any 'dummy' method to load the lib!
				//int[] cbrs = BassWma.BASS_WMA_EncodeGetRates(44100, 2, BASSWMAEncode.BASS_WMA_ENCODE_RATES_CBR);
				// now basswma.dll is loaded and the additional config options are available...

				if ( Bass.BASS_SetConfig(BASSConfig.BASS_CONFIG_WMA_PREBUF, 0) == false)
				{
					Console.WriteLine( "ERROR: " + Enum.GetName(typeof(BASSError), Bass.BASS_ErrorGetCode()) );
				}
				// we alraedy create the user callback methods...
				myStreamCreateURL = new DOWNLOADPROC(MyDownloadProc);
			}
			else
				MessageBox.Show(this, "Bass_Init error!" );	
		
		}
		
		void PlayStream(string streamURL)
		{
			if (streamURL == null)
				throw new ArgumentNullException("streamURL cannot be empty");
			Bass.BASS_StreamFree(_Stream);
			this.textBox1.Text = "";
			_url = streamURL;
			// test BASS_StreamCreateURL

			bool isWMA = false;
			if (_url != String.Empty)
			{
				this.textBox1.Text += "URL: "+_url+Environment.NewLine;
				// create the stream
                _Stream = Bass.BASS_StreamCreateURL(_url, 0, BASSFlag.BASS_STREAM_STATUS, myStreamCreateURL, IntPtr.Zero);
				if (_Stream == 0)
				{
					// try WMA streams...
					_Stream = BassWma.BASS_WMA_StreamCreateFile( _url, 0, 0, BASSFlag.BASS_DEFAULT );
					if (_Stream != 0)
						isWMA = true;
					else
					{
						// error
						this.statusBar1.Text = "ERROR...";
						return;
					}
				}
				_tagInfo = new TAG_INFO(_url);
				BASS_CHANNELINFO info = Bass.BASS_ChannelGetInfo(_Stream);
				if (info.ctype == BASSChannelType.BASS_CTYPE_STREAM_WMA)
					isWMA = true;
				// ok, do some pre-buffering...
				this.statusBar1.Text = "Buffering...";
				if (!isWMA)
				{
					// display buffering for MP3, OGG...
					while (true) 
					{ 
						long len = Bass.BASS_StreamGetFilePosition(_Stream, BASSStreamFilePosition.BASS_FILEPOS_END);
						if (len == -1)
							break; // typical for WMA streams
						// percentage of buffer filled
						float progress = ( 
							Bass.BASS_StreamGetFilePosition(_Stream, BASSStreamFilePosition.BASS_FILEPOS_DOWNLOAD) - 
							Bass.BASS_StreamGetFilePosition(_Stream, BASSStreamFilePosition.BASS_FILEPOS_CURRENT) 
							) * 100f / len;
                        
						if (progress > 75f) 
						{
							break; // over 75% full, enough
						}

						this.statusBar1.Text = String.Format( "Buffering... {0}%", progress );
					}
				}
				else
				{
					// display buffering for WMA...
					while (true) 
					{ 
						long len = Bass.BASS_StreamGetFilePosition(_Stream, BASSStreamFilePosition.BASS_FILEPOS_WMA_BUFFER);
						if (len == -1L)
							break;
						// percentage of buffer filled
						if (len > 75L) 
						{
							break; // over 75% full, enough
						}

						this.statusBar1.Text = String.Format( "Buffering... {0}%", len );
					}
				}

				// get the meta tags (manually - will not work for WMA streams here)
				string[] icy = Bass.BASS_ChannelGetTagsICY(_Stream);
				if (icy == null)
				{
					// try http...
					icy = Bass.BASS_ChannelGetTagsHTTP(_Stream);
				}
				if (icy != null)
				{
					foreach (string tag in icy)
					{
						this.textBox1.Text += "ICY: "+tag+Environment.NewLine;
					}
				}
				// get the initial meta data (streamed title...)
				icy = Bass.BASS_ChannelGetTagsMETA(_Stream);
				if (icy != null)
				{
					foreach (string tag in icy)
					{
						this.textBox1.Text += "Meta: "+tag+Environment.NewLine;
					}
				}
				else
				{
					// an ogg stream meta can be obtained here
					icy = Bass.BASS_ChannelGetTagsOGG(_Stream);
					if (icy != null)
					{
						foreach (string tag in icy)
						{
							this.textBox1.Text += "Meta: "+tag+Environment.NewLine;
						}
					}
				}

				// alternatively to the above, you might use the TAG_INFO (see BassTags add-on)
				// This will also work for WMA streams here ;-)
				if ( BassTags.BASS_TAG_GetFromURL( _Stream, _tagInfo) )
				{
					// and display what we get
					this.textBoxAlbum.Text = _tagInfo.album;
					this.textBoxArtist.Text = _tagInfo.artist;
					this.textBoxTitle.Text = _tagInfo.title;
					this.textBoxComment.Text = _tagInfo.comment;
					this.textBoxGenre.Text = _tagInfo.genre;
					this.textBoxYear.Text = _tagInfo.year;
				}

				// set a sync to get the title updates out of the meta data...
				mySync = new SYNCPROC(MetaSync);
                Bass.BASS_ChannelSetSync(_Stream, BASSSync.BASS_SYNC_META, 0, mySync, IntPtr.Zero);
                Bass.BASS_ChannelSetSync(_Stream, BASSSync.BASS_SYNC_WMA_CHANGE, 0, mySync, IntPtr.Zero);
				
				// start recording...
				int rechandle = 0;
				if ( Bass.BASS_RecordInit(-1) )
				{
					_byteswritten = 0;
					myRecProc = new RECORDPROC(MyRecoring);
                    rechandle = Bass.BASS_RecordStart(44100, 2, BASSFlag.BASS_RECORD_PAUSE, myRecProc, IntPtr.Zero);
				}
				this.statusBar1.Text = "Playling...";
				// play the stream
				Bass.BASS_ChannelPlay(_Stream, false);
				// record the stream
				Bass.BASS_ChannelPlay(rechandle, false);
			}
		}
		
	
			private int _byteswritten = 0;
		private byte[] _recbuffer = new byte[1048510]; // 1MB buffer
        private bool MyRecoring(int handle, IntPtr buffer, int length, IntPtr user)
		{
			// just a dummy here...nothing is really written to disk...
			if (length > 0 && buffer != IntPtr.Zero)
			{
				// copy from managed to unmanaged memory
				// it is clever to NOT alloc the byte[] everytime here, since ALL callbacks should be really fast!
				// and if you would do a 'new byte[]' every time here...the GarbageCollector would never really clean up that memory here
				// even other sideeffects might occure, due to the fact, that BASS micht call this callback too fast and too often...
				Marshal.Copy(buffer, _recbuffer, 0, length);
				// write to file
				// NOT implemented her...;-)
				_byteswritten += length;
				Console.WriteLine( "Bytes written = {0}", _byteswritten);
				if (_byteswritten < 800000)
					return true; // continue recording
				else
					return false;
			}
			return true;
		}

        private void MyDownloadProc(IntPtr buffer, int length, IntPtr user)
		{
			if (buffer != IntPtr.Zero && length == 0)
			{
				// the buffer contains HTTP or ICY tags.
				string txt = Marshal.PtrToStringAnsi(buffer);
				this.Invoke(new UpdateMessageDelegate(UpdateMessageDisplay), new object[] { txt });
				// you might instead also use "this.BeginInvoke(...)", which would call the delegate asynchron!
			}
		}

        private void MetaSync(int handle, int channel, int data, IntPtr user)
        {
            // BASS_SYNC_META is triggered on meta changes of SHOUTcast streams
            if (_tagInfo.UpdateFromMETA(Bass.BASS_ChannelGetTags(channel, BASSTag.BASS_TAG_META), false, true))
            {
                this.Invoke(new UpdateTagDelegate(UpdateTagDisplay));
            }
        }

        public delegate void UpdateTagDelegate();
        private void UpdateTagDisplay()
        {
            this.textBoxAlbum.Text = _tagInfo.album;
            this.textBoxArtist.Text = _tagInfo.artist;
            this.textBoxTitle.Text = _tagInfo.title;
            this.textBoxComment.Text = _tagInfo.comment;
            this.textBoxGenre.Text = _tagInfo.genre;
            this.textBoxYear.Text = _tagInfo.year;
        }

        public delegate void UpdateStatusDelegate(string txt);
        private void UpdateStatusDisplay(string txt)
        {
            this.statusBar1.Text = txt;
        }

		public delegate void UpdateMessageDelegate(string txt);
		private void UpdateMessageDisplay(string txt)
		{
			this.textBox1.Text += "Tags: " + txt + Environment.NewLine;
		}
		
	
		void Listen_Click(object sender, EventArgs e)
		{
		
		 ToolStripMenuItem przycisk =	 sender as ToolStripMenuItem;
			PlayStream((string)przycisk.Tag);
		}
	}
}
